package com.haiertvbic.tvnow;

import java.util.ArrayList;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.haiertvbic.tvnow.adapter.FunctionAdapter;
import com.haiertvbic.tvnow.adapter.FunctionAdapter.FunctionHodler;
import com.haiertvbic.tvnow.testblock.Function;
import com.haiertvbic.tvnow.util.SLog;

public class FunctionMake extends Activity {
	private ArrayList<String> fName = new ArrayList<String>();
	private ArrayList<Boolean> fStatus = new ArrayList<Boolean>();
	private static final String TAG = "FunctionMake";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_make_function_list);
		ListView commonLV = (ListView) findViewById(R.id.common_lv);

		getData();
		FunctionAdapter fAdapter = new FunctionAdapter(fName, fStatus, this);
		commonLV.setAdapter(fAdapter);
		commonLV.setOnItemClickListener(new FunctionItemClickListener());
	}

	private void getData() {
		fName.clear();
		fStatus.clear();
		for (Entry<String, Boolean> m : Function.function.entrySet()) {
			fName.add(m.getKey());
			fStatus.add(m.getValue());

			Editor toSP = getSharedPreferences("isLoad", Context.MODE_PRIVATE)
					.edit();
			toSP.putBoolean(m.getKey(), m.getValue());
			toSP.commit();
		}
	}

	class FunctionItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			FunctionHodler fHodler = (FunctionHodler) arg1.getTag();
			if (fStatus.get(arg2)) {
				Function.function.put(fName.get(arg2), false);
				fHodler.fStatus.setImageResource(R.drawable.function_close);
			} else {
				Function.function.put(fName.get(arg2), true);
				fHodler.fStatus.setImageResource(R.drawable.function_open);
			}
			getData();
		}

	}
}
